<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import dayjs from 'dayjs'
import PlanItemList from '@views/qms/plan/PlanItemList.vue'
import { getAction, postAction } from '@api/manage'
import ProductQualityStatusModal from './modules/ProductQualityStatusModal.vue'

export default {
  // 成品质量状态确认单
  name: 'FinishedProductQualityConfirm',
  components: { ProductQualityStatusModal, PlanItemList },
  mixins: [JeecgListMixin],
  data() {
    return {
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 40,
          align: 'center',
          fixed: 'left',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '计划单号',
          align: 'center',
          dataIndex: 'code'
        },
        {
          title: '生产车间',
          align: 'center',
          dataIndex: 'workshopCode_dictText'
        },
        {
          title: '执行日期',
          align: 'center',
          dataIndex: 'workTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '产品质量确认',
          align: 'center',
          dataIndex: 'productQuality'
        },
        {
          title: '制造生产过程确认',
          align: 'center',
          dataIndex: 'manufacturingProcess'
        },
        {
          title: '包装质量确认',
          align: 'center',
          dataIndex: 'packingQuality'
        },
        {
          title: '包装生产过程确认',
          align: 'center',
          dataIndex: 'packingProcess'
        },
        {
          title: '质量确认（合格品）',
          align: 'center',
          dataIndex: 'qualityConfirmedGood'
        },
        {
          title: '质量确认（返工品）',
          align: 'center',
          dataIndex: 'qualityConfirmedReturn'
        },
        {
          title: '成品库',
          align: 'center',
          dataIndex: 'finishedProductWarehouse'
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/pro/tslProWorkshopPlan/list',
        result: '/qms/inspection/plan/product-quality/result/queryList'
      },
      queryParam: {
        createTimeBegin: dayjs().subtract(7, 'day').format('YYYY-MM-DD'),
        createTimeEnd: dayjs().format('YYYY-MM-DD')
      },
      settingColumns: [],
      planItemMainId: ''
    }
  },
  methods: {
    loadData() {
      getAction(this.url.list).then(res => {
        let { result } = res
        if (result && result.records) {
          this.dataSource = result.records
          this.dataSourceBack = this.dataSource
          if (result.total) {
            this.ipagination.total = result.total
          } else {
            this.ipagination.total = 0
          }
          const codes = result.records.map(item => item.code)
          this.fetchResultData(codes)
        }
      })
    },
    fetchResultData(codes) {
      postAction(this.url.result, codes).then(res => {
        let { result } = res
        if (result) {
          this.dataSource.forEach(item => {
            const resultItemList = result.filter(resultItem => resultItem.planNumber === item.code)
            resultItemList.forEach(resultItem => {
              this.$set(item, resultItem.qualityProcess, resultItem.result)
              this.$set(item, 'qualityStatusId', resultItem.qualityStatusId)
            })
          })
        }
      })
    },
    handleConfirm(record) {
      this.$refs.productQualityStatusModal.confirm(record)
      this.$refs.productQualityStatusModal.title = '确认'
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.planItemMainId = selectionRows[0]['id']
    },
    printPreview(record) {
      let code = record.code
      window.open(`${window._CONFIG['domianURL']}/jmreport/view/1099174517737857024?code=${code}`, '_blank')
    }
  }
}
</script>

<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="制单日期">
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD"
                placeholder="请选择开始时间"
                class="query-group-cust"
                v-model="queryParam.createTimeBegin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD"
                placeholder="请选择结束时间"
                class="query-group-cust"
                v-model="queryParam.createTimeEnd"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <span style="float:right;">
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row style="width: 400px">
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!=='rowIndex'&& item.dataIndex!=='action'">
                        <a-col :span="12"><a-checkbox :value="item.dataIndex"><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />设置</a >
          </a-popover>
        </span>
      </div>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      class="j-table-force-nowrap1"
      :scroll="{x:true}"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type:'radio'}"
      :customRow="clickThenSelect"
      @change="handleTableChange">
      <span slot="action" slot-scope="text, record">
        <a @click="handleConfirm(record)">确认</a>
        <a-divider type="vertical" />
        <a @click="printPreview(record)">单据打印</a>
      </span>
    </a-table>

    <plan-item-list :mainId="planItemMainId" />
    <product-quality-status-modal ref="productQualityStatusModal" @ok="modalFormOk" />
  </a-card>
</template>

<style scoped>

</style>